﻿/// <reference path="script-new-engine-test.js" />
/// <reference path="Scripts/jquery-2.0.2.js" />

module("Minesweeper.Block");
test("Testing Block Creation ", function () {
    var testBlock = new MineSweeper.Block(5, 7);
    equal(testBlock.rowIndex, 5);
    equal(testBlock.colIndex, 7);
    equal(testBlock.initContent, "");
    equal(testBlock.isMine, false);
    equal(testBlock.isOpen, false);
    equal(testBlock.adjacentMines, 0);
});
test("Testing Block Render ", function () {
    var testBlock = new MineSweeper.Block(1,1);
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    $('.testDiv').append(testBlock.render());
    var renderedBlocksCount = $('.testDiv').find('td'); 
    $('.testDiv').remove();
    equal(renderedBlocksCount.length, 1); //error
});

module("Minesweeper.Row");
test("Testing Row Creation ", function () {
    var testRow = new MineSweeper.Row(5, 7);
    equal(testRow.rowIndex, 5);
    equal(testRow.colsCount, 7);
    equal(testRow.blocks.length, 7);
});
test("Testing Row Render ", function () {
    var testRow = new MineSweeper.Row(5, 7);
    var JQRowElement = testRow.render();
    var JQCellsInTheRow = JQRowElement.children();
    equal(JQCellsInTheRow.length, 7);
});

module("Minesweeper.Board");
test("Testing Board Creation ", function () {
    var testBoard = new MineSweeper.Board(10, 10);
    equal(testBoard.rows.length, 10);
    equal(testBoard.colsCount, 10);
});
test("Testing Board Render ", function () {
    var testBoard = new MineSweeper.Board(10, 10);
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    $('.testDiv').append(testBoard.render());
    var rowsCount = $('.testDiv').find('tr');
    equal(rowsCount.length, 10);
    var cellsCount = $('.testDiv').find('td');
    $('.testDiv').remove();
    equal(cellsCount.length, 100); 
});

module("Minesweeper");
test("Minesweeper Constr", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4,4,2);
    equal(testMineSweeper.rowsCount, 4);
    equal(testMineSweeper.colsCount, 4);
    equal(testMineSweeper.minesCount, 2);
    equal(testMineSweeper.allBlocks, 16);
    equal(testMineSweeper.remainingMines, 2);
    equal(testMineSweeper.blocksToOpen, 14);
    $('.testDiv').remove();
});
test("Minesweeper PutMines", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    equal(testMineSweeper.board.rows[3].blocks[3].isMine,false);
    equal(testMineSweeper.board.rows[1].blocks[1].isMine, true); //thats a mine
    $('.testDiv').remove();
});
test("Minesweeper CalculateAdjacentMines", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    equal(testMineSweeper.board.rows[0].blocks[1].adjacentMines, 2);
    equal(testMineSweeper.board.rows[0].blocks[0].adjacentMines, 1);
    equal(testMineSweeper.board.rows[3].blocks[3].adjacentMines, 0);
    equal(testMineSweeper.board.rows[1].blocks[1].adjacentMines, 0); //thats a mine
    $('.testDiv').remove();
});
test("Minesweeper IsInBoardRange", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    equal(testMineSweeper.isInBoardRange(2, 2), true);
    equal(testMineSweeper.isInBoardRange(4, 4), false);
    $('.testDiv').remove();
});
test("Minesweeper showAllMines", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    testMineSweeper.showAllMines();
    var mines = $(testDiv).find('.mine');
    $('.testDiv').remove();
    equal(mines.length, 2);
});
test("Minesweeper openAllEmptyAdjacentBlocks - click on empty cell",function(){
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    var cell=testMineSweeper.getCellByGivenCoordinates(3, 3);
    cell.addClass("target");
    $(".target").trigger("click");
    var openedCells = $(".open");
    equal(openedCells.length, 8);
    $('.testDiv').remove();
})
test("Minesweeper openAllEmptyAdjacentBlocks - click on non empty cell", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    var cell = testMineSweeper.getCellByGivenCoordinates(0, 3);
    cell.addClass("target");
    $(".target").trigger("click");
    var openedCells = $(".open");
    equal(openedCells.length, 1);
    $('.testDiv').remove();
})
test("Minesweeper openAllEmptyAdjacentBlocks3 - click on a mine", function () {
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testDiv";
    var testMineSweeper = new MineSweeper.Minesweeper(testDiv, 4, 4, 2);
    testMineSweeper.startGame();
    var cell = testMineSweeper.getCellByGivenCoordinates(1,1);
    cell.addClass("target");
    $(".target").trigger("click");
    var openedCells = $(".mine");
    equal(openedCells.length, 2);
    $('.testDiv').remove();
})

module("Scoreboard.Result");
test("Test Result Constr", function () {
    var testResult = new Scoreboard.Result("TestName", 200);
    equal(testResult.name, "TestName");
    equal(testResult.score, 200);
});
test("Test Result Render", function () {
    var testResult = new Scoreboard.Result("TestName", 200);
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "Div";
    $('.Div').append(testResult.render());
    var rowsCount = $('.Div').find('tr');
    equal(rowsCount.length, 1);
    var cellsCount = $('.Div').find('td');
    $('.testDiv').remove();
    equal(cellsCount.length, 2);
});

module("Scoreboard.Level");
test("Test Level Constr", function () {
    var testLevel = new Scoreboard.Level("test");
    equal(testLevel.title, "test");
    equal(testLevel.results.length, 0);
});
test("Test Level AddResult", function () {
    var testLevel = new Scoreboard.Level();
    testLevel.addResult("testName", 200);
    equal(testLevel.results.length, 1);
});
test("Test Level SortResult", function () {
    var testLevel = new Scoreboard.Level();
    testLevel.addResult("first", 200);
    testLevel.addResult("second", 100);
    testLevel.sortResults();
    var first = new Scoreboard.Result("second", 100);
    var second = new Scoreboard.Result("first", 200);
    equal(testLevel.results[0].name, first.name);
    equal(testLevel.results[0].score, first.score);
    equal(testLevel.results[1].name, second.name);
    equal(testLevel.results[1].score, second.score);
});
test("Test Level Render", function () {
    var testLevel = new Scoreboard.Level();
    testLevel.addResult("first", 200);
    var testDiv = document.createElement('div');
    document.body.appendChild(testDiv);
    testDiv.className = "testD";
    $('.testD').append(testLevel.render());
    var table = $('.testD').find('table');
    $('.testD').remove();
    equal(table.length, 1);
});

module("Scoreboard.Scoreboard");
test("Test Scoreboard Constr", function () {
    var test = new Scoreboard.Scoreboard("testElement",1);
    equal(test.element, "testElement");
    equal(test.resultsToShow, 1);
    equal(test.levels.length, 0);
});
test("Test Scoreboard AddLevel", function () {
    var test = new Scoreboard.Scoreboard();
    test.addLevel("testLevel");
    equal(test.levels.length, 1);
});
test("Test Scoreboard Save", function () {
    var testSave = new Scoreboard.Scoreboard();
    testSave.addLevel("Test");
    testSave.addLevel("Test2");
    testSave.save();
    var result = JSON.parse(localStorage.getItem("Scoreboard"));
    equal(result.length, 2);
});
test("Test Scoreboard Load", function () {
    var testLoad = new Scoreboard.Scoreboard();
    var ecpected = testLoad.addLevel("Test");
    testLoad.save();
    var result = new Scoreboard.Scoreboard().load();
    equal(result, ecpected);
});

module("Queue");
test("Queue Enque Function ", function () {
    var testQueue = new Queue();
    testQueue.enqueue(10);
    equal(testQueue.arr.length, 1);
    testQueue.enqueue(15);
    equal(testQueue.arr.length,2)
});
test("Queue Dequeue Function ", function () {
    var testQueue = new Queue();
    testQueue.enqueue(10);
    testQueue.enqueue(15);
    equal(testQueue.dequeue(), 10);
    equal(testQueue.arr.length, 1);
});
